<template>
    <vp-dialog ref="editDialogRef" :span="2">
        <template #title><vp-label zh="权限配置" en="Perm Config" align="left"/></template>
        <el-table :data="models" 
            v-loading="loading"
            ref="multipleTable" stripe border  style="width: 100%" row-key="id">
            <el-table-column align="center" label="#" type="index">
            </el-table-column>
            <el-table-column prop="jx" min-width="200">
                <template #header><vp-label zh="机型" en="A/C Model"/></template>
                <template #default="scope">
                     {{scope.row.jx}}
                </template>
            </el-table-column>
            <el-table-column prop="fjzch" min-width="500">
                <template #header><vp-label zh="飞机" en="A/C Reg"/></template>
                <template #default="scope">
                    <el-checkbox v-for="(item, index) in scope.row.aircrafts"  v-model="item.checked" :key="index" :true-label="1" :false-label="0" @change="changeAircraft(scope.row, item)">
                        {{item.fjzch}}
                    </el-checkbox>
                </template>
            </el-table-column>
        </el-table>
        <template #footer>
            <vp-btn-confirm :loading="saveLoading" @click="handleSave"/>
        </template>
    </vp-dialog>
</template>
<script setup lang="ts" name="editModel">
import { ref } from 'vue'
import request from '/@/utils/request';

const emits = defineEmits(['formSubmited']);   

const loading = ref(false);
const saveLoading = ref(false);
const id = ref("");
const models = ref([] as any[]);

const editDialogRef = ref();
const multipleTable = ref();
const initForm = ()=>{
    models.value = [];
}
const handleSave = () => {
    if(loading.value){
        return;
    }
    saveLoading.value = true;
    let list = [] as any[];
    models.value.forEach(model => {
        const hasChecked = (model.aircrafts||[]).some((aircraft: any)=> {return aircraft.checked === 1;});
        if (hasChecked) {
            const all = (model.aircrafts||[]).find((aircraft: any) => { return aircraft.id === '-'});
            if (all.checked === 1) {
                list.push({
                    jxid: model.id,
                    qjdbs: 10,
                });
            } else {
                list.push({
                    jxid: model.id,
                    qjdbs: 20,
                    aircrafts: (model.aircrafts||[]).filter((aircraft: any) => {
                        return aircraft.checked === 1 && aircraft.id !== '-' ;
                    }).map((aircraft: any) => {
                        return {
                            fjid: aircraft.id,
                        }
                    })
                });
            }
        }
    });
    request.post("/system/modelRole/permission/"+id.value, list).then(()=>{
        emits("formSubmited");
        editDialogRef.value.handleClose();
    }).catch(()=>{
    }).finally(()=>{
        saveLoading.value = false;
    })
}
const handleOpen = (id_: string)=>{
    id.value = id_;
    initForm();
    editDialogRef.value.handleOpen();
    loading.value = true;
    request.get(`/system/modelRole/permission/${id.value}`).then((res)=>{
        models.value = res.data.models || [];
        const permissions = res.data.permissions || [];
        models.value.forEach(model => {
            const permission = permissions.find((permission: any)=> {return permission.jxid === model.id;});
            model.aircrafts = model.aircrafts || [];
            model.aircrafts.unshift({id: '-', fjzch: '全机队'});
            model.aircrafts.forEach((aircraft: any) => {
                aircraft.checked = 0;
                if (permission?.qjdbs === 10
                    || (permission?.aircrafts || []).some((aircraftPermission: any)=> {
                    return aircraftPermission.fjid === aircraft.id;
                    })) {
                    aircraft.checked = 1;
                }
            });
            if (permission?.qjdbs === 10) {
                (model.aircrafts||[]).forEach((aircraft: any) => {
                    aircraft.checked = 1;
                });
            }
        });
        loading.value = false;
    })
}

const changeAircraft = (model: any, aircraft: any) => {
    if (aircraft.id === '-') {
        const checked = aircraft.checked;
        (model.aircrafts||[]).forEach((aircraft: any) => {
            aircraft.checked = checked;
        });
    } else {
        const all = (model.aircrafts||[]).find((aircraft: any) => { return aircraft.id === '-'});
        if (all) {
            if ((model.aircrafts||[]).some((aircraft: any) => { return aircraft.checked === 0;})) {
                all.checked = 0;
            }
        }
    }
}

defineExpose({
    handleOpen,
})

</script>
